package com.optimatch.ui.view.customComponents.bars;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import com.optimatch.ui.controler.localization.Constants;
import com.optimatch.ui.controler.localization.LanguageManager;

public class MenuPrincipal extends JMenuBar {

	private static final long serialVersionUID = 1L;

	private MenuPrincipal bar = this;
	
	private JMenu menu_file;
	private JMenu menu_view;
	private JMenu menu_help;
	
	private JMenuItem open_file;
	private JMenuItem load_file;
	
	private JFileChooser fc;
	
	private File file;
	
	public MenuPrincipal() {
		super();
		initMenu();
	}

	private void initMenu() {
		this.fc = new JFileChooser();
		
		this.menu_file = new JMenu(LanguageManager.getBundle().getString(Constants.FILE_MENU_NAME));
		this.menu_view = new JMenu(LanguageManager.getBundle().getString(Constants.VIEW_MENU_NAME));
		this.menu_help = new JMenu(LanguageManager.getBundle().getString(Constants.HELP_MENU_NAME));
		
		this.add(this.menu_file);
		this.add(this.menu_view);
		this.add(this.menu_help);
		
		this.open_file = new JMenuItem(LanguageManager.getBundle().getString(Constants.OPEN_FILE_MENU_ITEM));
		this.load_file = new JMenuItem(LanguageManager.getBundle().getString(Constants.LOAD_FILE_MENU_ITEM));
		this.load_file.setEnabled(false);
		
		this.menu_file.add(this.open_file);
		this.menu_file.addSeparator();
		this.menu_file.add(this.load_file);
		

		
		this.open_file.addMouseListener( new MouseAdapter(){
					@Override
					public void mousePressed(MouseEvent e) {
						
						int returnVal = fc.showOpenDialog(bar.getParent());
					    
						if (returnVal == JFileChooser.APPROVE_OPTION) {
				            bar.setFile(fc.getSelectedFile());
				            load_file.setEnabled(true);
				        }
						
					}
			
		});
		
	}
	
	public void setFile(File nFile){
		file = nFile;
	}
	
	public File getFile(){return file;}
	
	public JMenuItem getLoadFile() { return load_file; }
	
	public void reloadJMenuItemLoad(){
		this.load_file.setText(LanguageManager.getBundle().getString(Constants.LOAD_FILE_MENU_ITEM));
		this.load_file.setEnabled(false);
	}
	
}
